package com.zyk.scaffold.seata.order.business.order.service.impl;

import com.zyk.scaffold.common.utils.AssertUtil;
import com.zyk.scaffold.seata.order.business.order.pojo.request.OrderRequest;
import com.zyk.scaffold.seata.order.business.order.service.OrderService;
import com.zyk.scaffold.seata.order.repositorys.entity.OrderTbl;
import com.zyk.scaffold.seata.order.repositorys.interfaces.OrderTblRepository;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderTblRepository orderTblRepository;

    @Override
    @GlobalTransactional
    public Boolean ok(OrderRequest orderRequest) {
        OrderTbl orderTbl = new OrderTbl();
        orderTbl.setCommodityCode(orderRequest.getCommodityCode());
        orderTbl.setMoney(orderRequest.getMoney());
        orderTbl.setCount(orderRequest.getCount());
        orderTbl.setUserId(orderRequest.getUserId());
        return orderTblRepository.save(orderTbl);
    }

    @Override
    @GlobalTransactional
    public Boolean error(OrderRequest orderRequest) {
        OrderTbl orderTbl = new OrderTbl();
        orderTbl.setCommodityCode(orderRequest.getCommodityCode());
        orderTbl.setMoney(orderRequest.getMoney());
        orderTbl.setCount(orderRequest.getCount());
        orderTbl.setUserId(orderRequest.getUserId());
        boolean save = orderTblRepository.save(orderTbl);
        AssertUtil.error("创建订单失败！");
        return save;
    }

    @Override
    @Transactional
    public Boolean none_seata_ok(OrderRequest orderRequest) {
        OrderTbl orderTbl = new OrderTbl();
        orderTbl.setCommodityCode(orderRequest.getCommodityCode());
        orderTbl.setMoney(orderRequest.getMoney());
        orderTbl.setCount(orderRequest.getCount());
        orderTbl.setUserId(orderRequest.getUserId());
        return orderTblRepository.save(orderTbl);
    }

    @Override
    @Transactional
    public Boolean none_seata_error(OrderRequest orderRequest) {
        OrderTbl orderTbl = new OrderTbl();
        orderTbl.setCommodityCode(orderRequest.getCommodityCode());
        orderTbl.setMoney(orderRequest.getMoney());
        orderTbl.setCount(orderRequest.getCount());
        orderTbl.setUserId(orderRequest.getUserId());
        boolean save = orderTblRepository.save(orderTbl);
        AssertUtil.error("创建订单失败！");
        return save;
    }
}
